import deviceCategoryChart from './device-category-chart.vue'
import payCategoryChart from './pay-category-chart.vue'
import areaDeviceChart from './area-device-chart.vue'
import chargeTimeChart from './charge-time-chart.vue'
import networkCategoryChart from './network-category-chart.vue'
import gatewayCategoryChart from './gateway-category-chart.vue'
import latelyUseCountChart from './lately-use-count-chart.vue'
import latelyConsumePowerChart from './lately-consume-power-chart.vue'
import collectChart from './collect-chart.vue'

import { Request } from '@/utils'

export default {
    data () {
        return {
            deviceCategoryData : null, //设备类型
            payCategoryData : null, //支付类型
            areaDeviceData : null, //地域设备
            chargeTimeData : null, //充电时长

            networkCategoryData : null, //通讯类型
            gatewayCategoryData : null, //网关类型

            latelyUseCountData : null, //最近七天使用次数
            latelyConsumePowerData : null, //最近七天消耗电量

            collectData : null, //收藏数
        }
    },

    components : {
        deviceCategoryChart,
        payCategoryChart,
        areaDeviceChart,
        chargeTimeChart,

        networkCategoryChart,
        gatewayCategoryChart,

        latelyUseCountChart,
        latelyConsumePowerChart,
        collectChart
    },

    methods : {
        run () {
            (new Request()).get('statistic/device/totalDeviceByVehicleType', res => { //获取设备
                var deviceCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        var name = this.$store.state.vehicleTypeList[item.vehicleType]
                        deviceCategoryData.push({name : name, value : item.deviceNum})
                    })
                }
                
                this.deviceCategoryData = deviceCategoryData
            });

            (new Request()).get('statistic/device/totalDevicePayType', res => { //获取支付方式
                var payTypes = {1 : '微信', 2 : '投币', 3 : '刷卡', 4 : '支付宝'}
                var payCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        var name = payTypes[item.payType]
                        payCategoryData.push({name : name, value : item.deviceNum})
                    })
                }
                
                this.payCategoryData = payCategoryData
            });

            (new Request()).get('statistic/device/totalDeviceAreaOnlineNum', res => { //获取地域设备
                var areaDeviceData = {
                    area : [],
                    data : [
                        [],
                        []
                    ]
                }
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        areaDeviceData.area.push(item.areaName)
                        areaDeviceData.data[0].push(item.totalDeviceNum)
                        areaDeviceData.data[1].push(item.onlineNum)
                    })
                }

                this.areaDeviceData = areaDeviceData
            });


            (new Request()).get('statistic/user/totalSevenDaysChargeTime', res => { //获取充电时长
                var chargeTimeData = {
                    date : [],
                    data : [
                        [],
                        []
                    ]
                }
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        chargeTimeData.date.push(item.date)
                        chargeTimeData.data[0].push(((item.consumeQuantity || 0) / 60).toFixed(2))
                        chargeTimeData.data[1].push(((item.avgConsumeQuantity || 0) / 60).toFixed(2))
                    })
                }
                
                this.chargeTimeData = chargeTimeData
            });

            (new Request()).get('statistic/device/totalDeviceNetworkType', res => { //获取通讯类型
                var networkType = {1 : '无线', 2 : 'WIFI', 3 : '网线', 4 : 'NB'}
                var networkCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        networkCategoryData.push({name : networkType[item.type], value : (item.num / item.totalDeviceNum).toFixed(0)})
                    })
                }
                this.networkCategoryData = networkCategoryData
            });

            (new Request()).get('statistic/device/totalDeviceGatewayType', res => { //获取网关
                var gatewayCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        gatewayCategoryData.push({name : item.gatewayName, value : item.num})
                    })
                }
                this.gatewayCategoryData = gatewayCategoryData
            });

            (new Request()).get('statistic/user/totalSevenDaysChargeNum', res => { //获取近七天使用次数
                var latelyUseCountData = {
                    date : [],
                    data : [
                        [], //电单车
                        [], //汽车
                        [], //换电柜
                        [], //充电柜
                    ]
                }
                
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        latelyUseCountData.date.push(item.date)

                        latelyUseCountData.data[0].push(item.electrocar || 0)
                        latelyUseCountData.data[1].push(item.car || 0)
                        latelyUseCountData.data[2].push(item.exchangeCabinet || 0)
                        latelyUseCountData.data[3].push(item.chargeCabinet || 0)
                    })
                }
                this.latelyUseCountData = latelyUseCountData

            });

            (new Request()).get('statistic/device/totalSevenDaysChargeEnergy', res => { //获取近七天消耗电量
                var latelyConsumePowerData = {
                    date : [],
                    data : [
                        [], //电单车
                        [], //汽车
                        [], //换电柜
                        [], //充电柜
                    ]
                }
                
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        latelyConsumePowerData.date.push(item.date)

                        latelyConsumePowerData.data[0].push(item.electrocar || 0)
                        latelyConsumePowerData.data[1].push(item.car || 0)
                        latelyConsumePowerData.data[2].push(item.exchangeCabinet || 0)
                        latelyConsumePowerData.data[3].push(item.chargeCabinet || 0)
                    })
                }
                this.latelyConsumePowerData = latelyConsumePowerData

            });

            (new Request()).get('statistic/device/totalDeviceCollectNum', res => { //获取收藏
                var collectData = []
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        if(item.collectNum > item.totalDeviceNum) {
                            item.totalDeviceNum = item.collectNum
                        }
                        collectData.push({name : this.$store.state.vehicleTypeList[item.type], total : item.totalDeviceNum, collected : item.collectNum})
                    })
                }

                this.collectData = collectData

            });

            setTimeout(() => {
                this.run()
            }, 30 * 60 * 1000)
        }
    },

    mounted () {
        this.run()
    }
}